﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WebUI.Models;
using System.Net;

namespace Citi.OTFPC.PP.ProcessDashboard.ConsoleInterface.Util
{
    public class FileHelper
    {

        public static string GetNewestFile(string directory, Periodicity periodicy, string filePrefix = null, string extension = null)
        {
            var files = Directory.GetFiles(directory);

            if (files.Length == 0) return null;

            var dias = 0;

            switch (periodicy)
            {
                case Periodicity.Weekly:
                    dias = 7;
                    break;
                case Periodicity.Monthly:
                    dias = 30;
                    break;
            }

            return SelectFile(directory, DateTime.Today.AddDays(-dias), filePrefix, extension, files);

        }

        public static string GetNewestFileAt(string directory, DateTime dateTime, string filePrefix = null, string extension = null)
        {

            var files = Directory.GetFiles(directory);

            if (files.Length == 0) return null;

            return SelectFile(directory, dateTime, filePrefix, extension, files);

        }

        public static string GetPriorNewestFileAt(string directory, string filePrefix = null, string extension = null)
        {
            var files = Directory.GetFiles(directory);

            if (files.Length == 0) return null;

            var newestFile = files.Where(x => (filePrefix == null || Path.GetFileNameWithoutExtension(x).StartsWith(filePrefix))
                                               && (extension == null || Path.GetExtension(x) == extension)).OrderByDescending(x => File.GetCreationTime(x).Date)
                                               .Take(2).LastOrDefault(); 

            return newestFile != null ? Path.Combine(directory, newestFile) : null;
        }

        public static string GetNewestFileAt(string directory, string filePrefix = null, string extension = null)
        {
            var files = Directory.GetFiles(directory);

            if (files.Length == 0) return null;

            var newestFile = files.Where(x => (filePrefix == null || Path.GetFileNameWithoutExtension(x).StartsWith(filePrefix))
                                               && (extension == null || Path.GetExtension(x) == extension)).OrderByDescending(x => File.GetCreationTime(x).Date)
                                               .FirstOrDefault();

            return newestFile != null ? Path.Combine(directory, newestFile) : null;
        }

        private static string SelectFile(string directory, DateTime dateTime, string filePrefix, string extension, IEnumerable<string> files)
        {
            var newestFile = files.LastOrDefault(x => File.GetCreationTime(x).Date >= dateTime.Date
                                                      && (filePrefix == null || Path.GetFileNameWithoutExtension(x).StartsWith(filePrefix))
                                                      && (extension == null || Path.GetExtension(x) == extension));

            return newestFile != null ? Path.Combine(directory, newestFile) : null;
        }

    }
}